import { HOST } from '../../common/constant'
import { dowImg } from '../../common/method'
import { apiAutoUpdateBgDownNumber } from '../../common/api'
let isShare = false
let currentImgUrl = '';
Page({
  data: {
    checkUrl: '',
    showDialog: false,
    statusBarHeight:0,
    dataList: [],
    dataList2: [],
    dataList3: [],
    dataList4: [],
    dataList5: [], // 可爱
    host: HOST
  },
  getListInfo(page=0, groupCurrent=0) {
    return new Promise((resolve, reject) => {
      wx.request({
        method: "GET",
        url: `${HOST}/background?type=${groupCurrent}&page=${page}`,
        data: {},
        header: { 'content-type': 'application/json' },
        success: (res) => {
          const { data } = res
          resolve(data)
        }
      })
    })
  },
  getWindowInfos() {
    const { statusBarHeight } = wx.getWindowInfo()
    this.setData({
      statusBarHeight
    })
  },

  async itemClick(e) {
    const { mark } = e
    const sourceUrl = mark['source-url']
    currentImgUrl = sourceUrl
    this.getTabBar((tabbar) => {
      tabbar.setData({
        tabBarActive: false
      })
    })

    wx.showLoading({
      title: '加载中',
      mask: true
    })
    this.setData({
      checkUrl: ''
    })
    wx.hideLoading()
    setTimeout(() => {
      this.setData({
        showDialog: true,
        checkUrl: sourceUrl
      })
    }, 500)
  },
  dialogClose() {
    this.getTabBar((tabbar) => {
      tabbar.setData({
        tabBarActive: true
      })
    })
    this.setData({
      showDialog: false
    })
  },
  async dowImg() {
    if (isShare) {
      dowImg({ imgUrl: currentImgUrl })
    } else {
      wx.showToast({
        title: '请先分享才可以下载哦',
        icon: 'none',
        mask: true
      })
    }
  },
  goMorePage() {
    wx.navigateTo({
      url: '/pages/background/index',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.getWindowInfos()
    this.getListInfo().then((res) => {
      
      res.forEach(async (item, index) => {
        const { url } = item
        item.thumb = url.replace('images/', 'images/compressed_')
      })
        this.setData({
          dataList: res,
        })
    })
    this.getListInfo(0, 5).then((res)=>{
      res.forEach(async (item, index) => {
        const { url } = item
        item.thumb = url.replace('images/', 'images/compressed_')
      })
      this.setData({
        dataList2: res,
      })
    })
    this.getListInfo(0, 3).then((res)=>{
      res.forEach(async (item, index) => {
        const { url } = item
        item.thumb = url.replace('images/', 'images/compressed_')
      })
      this.setData({
        dataList3: res,
      })
    })
    this.getListInfo(0, 6).then((res)=>{
      res.forEach(async (item, index) => {
        const { url } = item
        item.thumb = url.replace('images/', 'images/compressed_')
      })
      this.setData({
        dataList4: res,
      })
    })
// 可爱
    this.getListInfo(0, 11).then((res)=>{
      res.forEach(async (item, index) => {
        const { url } = item
        item.thumb = url.replace('images/', 'images/compressed_')
      })
      this.setData({
        dataList5: res,
      })
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    isShare = true
    apiAutoUpdateBgDownNumber({
      userId: wx.getStorageSync('userSignature')
    })
    return {
      title: '来选一张精美的壁纸吧'
    }
  }
})